<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/7
 * Time: 11:07
 */

namespace app\admin\controller;


use app\admin\helper\AclHelper;
use app\admin\model\EmailM;
use app\admin\model\LogsM;
use app\admin\model\ResourceButtonM;
use app\admin\model\ResourceM;
use app\admin\model\RoleM;
use app\admin\model\UserM;
use think\Config;

class System extends Base
{

    protected $userM;
    protected $logsM;
    protected $roleM;
    protected $resourceM;
    protected $resourceButtonM;
    protected $emailM;

    /*
    * 依赖注入Model
    * */
    public function __construct(UserM $userM,LogsM $logsM,RoleM $roleM,ResourceM $resourceM,ResourceButtonM $resourceButtonM,EmailM $emailM){
        parent::__construct();
        $this->userM = $userM;
        $this->logsM = $logsM;
        $this->roleM = $roleM;
        $this->resourceM = $resourceM;
        $this->resourceButtonM = $resourceButtonM;
        $this->emailM = $emailM;
    }

    /**
     * @return array|mixed
     */
    public function email_config(){
        if ($this->request->isPost()){
            $validate = $this->emailM->validate_model();
            if ($validate['code']){
                return $this->emailM->config();
            }else{
                return $validate;
            }
        }
        $this->assign("lang_filed",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("data",$this->emailM->listData());
        return $this->fetch('system/email/config');
    }

    /**
     * controller-action acl
     */
    public function ca_acl(){
        if ($this->request->isPost()){
            return $this->resourceM->auth();
        }
        $role_id = input('get.role_id/s');
        $this->assign("controllers",AclHelper::buildCA());
        $this->assign("lang_filed",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("role",$this->roleM->getByRoleId($role_id));
        $this->assign("auth",json_encode($this->resourceM->listAll($role_id)));
        return $this->fetch('system/role/ca');
    }

    /**
     * button view acl
     */
    public function button_acl(){
        if ($this->request->isPost()){
            return $this->resourceButtonM->auth();
        }
        $role_id = input('get.role_id/s');
        $this->assign("modules",AclHelper::buildButton());
        $this->assign("lang_filed",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("role",$this->roleM->getByRoleId($role_id));
        $this->assign("auth",json_encode($this->resourceButtonM->listAll($role_id)));
        return $this->fetch('system/role/button');
    }

    /**
     * 列表
     */
    public function role_list(){
        if ($this->request->isPost() || input('get.export/b')){
            return $this->roleM->listData();
        }
        $this->assign("__search",$this->roleM->__search());
        $this->assign("__header",$this->roleM->__header());
        return $this->fetch('system/role/list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function role_form(){
        if ($this->request->isPost()) {
            $method = input('post.method/s');
            if ($method == 'edit') {
                $validate = $this->roleM->validate_model('edit');
                if ($validate['code']){
                    return $this->roleM->edit();
                }else{
                    return $validate;
                }
            }else if ($method == 'add') {
                $validate = $this->roleM->validate_model('add');
                if ($validate['code']){
                    return $this->roleM->add();
                }else{
                    return $validate;
                }
            }
            return op_fail();
        }
        $this->assign("lang_filed",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $this->assign("data",$this->roleM->getById(input('get.id/d')));
        return $this->fetch('system/role/form');
    }

    /**
     * 删除
     */
    public function role_del(){
        return $this->roleM->del();
    }

    /**
     * 列表
     */
    public function user_list(){
        if ($this->request->isPost()|| input('get.export/b')){
            return $this->userM->listData();
        }
        $this->assign("__search",$this->userM->__search());
        $this->assign("__header",$this->userM->__header());
        return $this->fetch('system/user/list');
    }

    /**
     * 新增
     * 修改
     * @return mixed
     */
    public function user_form(){
        if ($this->request->isPost()) {
            $method = input('post.method/s');
            if ($method == 'edit') {
                $validate = $this->userM->validate_model('edit');
                if ($validate['code']){
                    return $this->userM->edit();
                }else{
                    return $validate;
                }
            }else if ($method == 'add') {
                $validate = $this->userM->validate_model('add');
                if ($validate['code']){
                    return $this->userM->add();
                }else{
                    return $validate;
                }
            }
            return op_fail();
        }
        $this->assign("is_role",input('get.is_role/s'));
        $this->assign("role_list",$this->roleM->listAll());
        $this->assign("lang_filed",Config::get('lang_field_'.$this->curr_lang));
        $this->assign("method",input('get.method/s'));
        $this->assign("data",$this->userM->getById(input('get.id/d')));
        return $this->fetch('system/user/form');
    }

    /**
     * 删除
     */
    public function user_del(){
        return $this->userM->del();
    }

    /**
     * 列表
     * @return \app\admin\popo\LayData|mixed
     */
    public function log_list(){
        if ($this->request->isPost() || input('get.export/b')){
            return $this->logsM ->listData();
        }
        $this->assign("__search",$this->logsM->__search());
        $this->assign("__header",$this->logsM->__header());
        return $this->fetch('system/log/list');
    }

}